package com.qcby.controller;

import com.qcby.entity.*;
import com.qcby.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author asheng
 * @date 09点28分
 */
@Controller
@RequestMapping("student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private PlanCourseService planCourseService;
    @Autowired
    private StuCourseService stuCourseService;
    @Autowired
    private CourseReadyService courseReadyService;
    @Autowired
    private StuTeacherPlanCourseService stuTeacherPlanCourseService;


    @RequestMapping("student_index")
    public String student_index(){

        return "student_index";
    }
    @RequestMapping("t_index")
    public String t_index(Integer msg,Model model){

        return "t_index";
    }
    /**
     * 登录界面
     * @return
     */
    @RequestMapping("student_login")
    public String student_login(Integer m,Model model){
        if(m==null) {
            return "student_login";
        }
        if(m==6){
            model.addAttribute("m",m);
            return "student_login";
        }
        return "student_login";
    }

    /**
     * 登录方法,跳转到登录成功的界面
     * @param student
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("student_gologin")
    public String student_gologin(Student student, Model model, HttpServletRequest request) {

        List<Student> students = studentService.student_gologin(student);
        if (students != null && !students.isEmpty() ){
            HttpSession session = request.getSession();
            session.setAttribute("stu",students);
            model.addAttribute("student",students.get(0));
            model.addAttribute("msg","登陆成功!");
            return "student";
        }
        model.addAttribute("msg","登陆失败!账号或密码错误!!");
            return"student_login";
    }

    /**
     * 跳转到教学计划界面
     * @param planCourse
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("student_plan")
    public String student_plan(PlanCourse planCourse, Model model, HttpSession session){
        List<Student> students = (List<Student>) session.getAttribute("stu");
        System.out.println(students);
        model.addAttribute("student",students.get(0));

        List<PlanCourse> findMinUp = planCourseService.findMinUp();
        model.addAttribute("minListUp",findMinUp);
        List<PlanCourse> findMinDown = planCourseService.findMinDown();
        model.addAttribute("minListDown",findMinDown);
        List<PlanCourse> findMidUp = planCourseService.findMidUp();
        model.addAttribute("midListUp",findMidUp);
        List<PlanCourse> findMidDown = planCourseService.findMidDown();
        model.addAttribute("midListDown",findMidDown);
        List<PlanCourse> findMaxUp = planCourseService.findMaxUp();
        model.addAttribute("maxListUp",findMaxUp);
        List<PlanCourse> findMaxDown = planCourseService.findMaxDown();
        model.addAttribute("maxListDown",findMaxDown);
        return "student_plan";
    }

    /**
     * 跳转选课界面并实现查询
     */
    @RequestMapping("student_select")
    public String student_select(StuTeacherPlanCourse teacherPlanCourse, Model model, HttpSession session){
        //用多表连接join,通过teacherPlanCourse作为中间关联的来获取对应的课程与对应上课的教师
        //再基础上用where筛选出选修与必修
        List<StuCourse> courseB = stuCourseService.findjoinB();
        System.out.println(courseB);
        model.addAttribute("allListB",courseB);
        List<StuCourse> courseX = stuCourseService.findjoinX();
        model.addAttribute("allListX",courseX);
        return "student_select";
    }

    /**
     * 选课按钮实现
     * @return
     */
    @RequestMapping("course_select")
    public String course_select(Model model,HttpServletRequest request,HttpSession session,Integer course_no,Integer tea_account,String course_start){
        System.out.println(course_no+"1111111111111111111"+tea_account);
        //course_no是从前端页面获取的当前要选择的课序号
        //tea_account是从前端页面获取到的对应要选的课的任课教师
        //course_start是从前端页面获取到的对应的上课的时间
        //在teacherplancourse里查出course_no和tea_account
        List<TeacherPlanCourse> i = stuTeacherPlanCourseService.selectTeacherId(course_no,tea_account);
        System.out.println(i+"----------------");
        Integer teacherPlanCourse = i.get(0).getId();//获取teacherPlanCorse里的id
        List<Student> students = (List<Student>) session.getAttribute("stu");
        Integer stu_account = students.get(0).getStu_account();//获取到学号

        List<CourseReady> c = courseReadyService.selectCourseNo(stu_account,course_no);//通过学号来查找，这个学生已选择的课
        System.out.println(c);
        if(c.isEmpty()){//如果查询的返回结果为空，那么证明该学生没有选过这门课。可以选
            //再判断时间是否冲突
            System.out.println("无同课冲突");
            List<CourseReady> t = courseReadyService.selectStart(stu_account, course_start);
            if(t.isEmpty()){//证明,这个时间还没有被占用,可以添加课程
                //往里添加
                System.out.println("无时间冲突");
                int n = courseReadyService.insertInToCourseRe(teacherPlanCourse, stu_account);
                if(n==0){
                    model.addAttribute("msg","选课失败");//选课失败
                    //再次列出所有课
                    List<StuCourse> courseB = stuCourseService.findjoinB();
                    model.addAttribute("allListB",courseB);
                    List<StuCourse> courseX = stuCourseService.findjoinX();
                    model.addAttribute("allListX",courseX);
                    return "student_select";
                }else{//证明选课成功
                    System.out.println("选课成功");
                    model.addAttribute("msg","选课成功");//选课成功
                    //再次列出所有课
                    List<StuCourse> courseB = stuCourseService.findjoinB();
                    model.addAttribute("allListB",courseB);
                    List<StuCourse> courseX = stuCourseService.findjoinX();
                    model.addAttribute("allListX",courseX);
                    return "student_select";
                }
            }else{//证明这个时间已经有了别的课
                System.out.println("时间冲突");
                model.addAttribute("msg","上课时间冲突");
                //再次列出所有课
                List<StuCourse> courseB = stuCourseService.findjoinB();
                model.addAttribute("allListB",courseB);
                List<StuCourse> courseX = stuCourseService.findjoinX();
                model.addAttribute("allListX",courseX);
                return "student_select";
            }
        }else{//证明该学生已经选过这门课了，不可以重复选课
            System.out.println("选过同样的课");
            model.addAttribute("msg","你已经选过该门课程，不可重复添加");
            //再次列出所有课
            List<StuCourse> courseB = stuCourseService.findjoinB();
            model.addAttribute("allListB",courseB);
            List<StuCourse> courseX = stuCourseService.findjoinX();
            model.addAttribute("allListX",courseX);
            return "student_select";
        }
    }
    /**
     * 选课时查询按钮实现
     * 按课程号和课程名查询
     * @param model
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping("course_find")
    public String course_find(Model model, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String course = request.getParameter("course");
        System.out.println(course);
        List<StuCourse> findCourseByIdName = stuCourseService.findByIdName(course);
        System.out.println(findCourseByIdName);
        //查到的数据不空时返回的结果
        if(!findCourseByIdName.isEmpty()){
            model.addAttribute("course",findCourseByIdName);
            return "student_find";
        }else{
            model.addAttribute("msg","查找不到");
            //将查不到数据信息返回到页面弹窗
            //再查询课程,回到选课页面
            List<StuCourse> courseB = stuCourseService.findjoinB();
            //System.out.println(courseB);
            model.addAttribute("allListB",courseB);
            List<StuCourse> courseX = stuCourseService.findjoinX();
            model.addAttribute("allListX",courseX);
            return "student_select";
            //想用页面重定向,但是用了不能弹框
            //return "redirect:student_select";
        }
    }

    /**
     * 跳转选课结果
     * @return
     */
    @RequestMapping("student_result")
    public String student_result( Model model,HttpSession session){
        List<Student> student = (List<Student>) session.getAttribute("stu");
        //获取stu_account
        Integer stu_account = student.get(0).getStu_account();

        List<StuCourse> findStuCourse = stuCourseService.findStuCourse(stu_account);
        System.out.println(findStuCourse);
        model.addAttribute("listReady",findStuCourse);
        return "student_result";
    }


    /**
     * 退课
     * @param model
     * @return
     */
    @RequestMapping("deleteCourse")
    public String deleteCourse(Integer planCourse_id,Integer tea_account, Model model,HttpSession session){
        List<Student> stu = (List<Student>) session.getAttribute("stu");
        List<StuTeacherPlanCourse> teacherPlanCourses = stuTeacherPlanCourseService.selectId(planCourse_id, tea_account);
       // System.out.println(teacherPlanCourses);
        Integer id = teacherPlanCourses.get(0).getId();
       // System.out.println(id);//获取到要删的teacherPlanCourse的id
        courseReadyService.deleteById(id, stu.get(0).getStu_account());

        return "redirect:student_result";
    }
    /**
     * 修改密码
     */
    @RequestMapping("student_update")
    public String student_updata(String stu_password,Student student, Model model,HttpSession session) {
        //System.out.println("是否获取密码"+stu_password);
        List<Student> students = (List<Student>) session.getAttribute("stu");
        model.addAttribute("student",students.get(0));
        students.get(0).getStu_account();
        if(stu_password==null){
            return "student_update";
        }else{
            int i = studentService.student_update(stu_password, students.get(0).getStu_account());
            model.addAttribute("msg",000);
            return "student_update";//回到修改密码界面
        }
    }

}